home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Power Programmierung
/
Power-Programmierung (Tewi)(1994).iso
/
magazine
/
pcmagazi
/
1992
/
04
/
list.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
1992-02-05
|
2KB
|
101 lines
// list.cpp RHS 8/2/91
#include<stdio.h>
#include"list.h"
List::List(void)
{
lastnode = end = start = current = NULL;
NumElements = lastndx = 0;
}
List::~List(void)
{
if(start)
{
NODE *temp;
for(current = start; current; )
{
temp = current->next;
delete current->object;
delete current;
current = temp;
}
}
}
int List::Add(void *object)
{
NODE *temp = new NODE;
if(!temp)
return 1;
temp->object = object;
temp->next = NULL;
if(!start)
lastnode = start = end = current = temp;
else
{
end->next = temp;
current = end = temp;
}
NumElements++;
return 0;
}
void *List::Current(void)
{
return current->object;
}
int List::Num(void)
{
return NumElements;
}
void *List::operator[](int index)
{
if(index >= NumElements)
return NULL;
int i = 0;
NODE *temp = start;
if(lastndx <= index)
{
i = lastndx;
temp = lastnode;
}
for( ; i < index; i++)
temp = temp->next;
lastndx = i;
lastnode = temp;
return temp->object;
}
void List::Dump(int index)
{
if(index >= NumElements)
return;
int i = 0;
NODE *temp = start;
if(lastndx <= index)
{
i = lastndx;
temp = lastnode;
}
for( ; i < index; i++)
temp = temp->next;
lastndx = i;
lastnode = temp;
printf("(index= %d) NODE @ %p: object=%p next=%p\n",
index,temp,temp->object,temp->next);
}